﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

namespace Proggy.Web.Cms.Core.Models
{
    /// <summary>
    /// Model for drag and drop item
    /// </summary>
    public class AddedDragAndDropItem
    {
        /// <summary>
        /// The part name alias
        /// </summary>
        [Required(AllowEmptyStrings=false)]
        public string PartName { get; set; }
        
        /// <summary>
        /// The unique id for this added part
        /// </summary>
        [Required(AllowEmptyStrings=false)]
        public string UniqueId { get; set; }

        /// <summary>
        /// The node id where it resides
        /// </summary>
        public int NodeId { get; set; }

        /// <summary>
        /// The saved config / value data
        /// </summary>
        public string ConfigData { get; set; }

        /// <summary>
        /// The width (eg "60em")
        /// </summary>
        public string Width { get; set; }

        /// <summary>
        /// The margin (eg "0 0 0 10em")
        /// </summary>
        public string Margin { get; set; }

        /// <summary>
        /// The float (eg "none", "left")
        /// </summary>
        public string Float { get; set; }

        /// <summary>
        /// The clear (eg "none", "left", "both")
        /// </summary>
        public string Clear { get; set; }

        /// <summary>
        /// Returns the model as json
        /// </summary>
        /// <returns></returns>
        public string GetModelAsJson()
        {
            // 
            return JsonConvert.SerializeObject(this);
        }

        private string[] _marginArray;
        public string[] MarginArray { get {
            if (this._marginArray == null) {
                this._marginArray = this.Margin.Split(' ');
            }

            return this._marginArray;
        } }

        public int getMarginInt(int index) {
            return Convert.ToInt32(MarginArray[index].Substring(0, MarginArray[index].Length - 2));
        }

        public int GetMarginBottom()
        {
            // 
            return this.getMarginInt(2);
        }

        public int GetMarginLeft()
        {
            //
            return this.getMarginInt(3);
        }

        public int GetMarginRight()
        {
            return this.getMarginInt(1);
        }

        public int GetMarginTop()
        {
            return this.getMarginInt(0);
        }

        public string GetMarginType()
        {
            // get first
            var first = this.MarginArray[0];

            // get end
            return first.Substring(first.Length - 2,2);
        }

        public int GetWidth()
        {
            if (this.Width == "auto")
            {
                return 0;
            }

            return Convert.ToInt32(this.Width.Substring(0, this.Width.Length - 2));
        }

        public string GetWidthType()
        {
            return Width.Substring(Width.Length - 2, 2);
        }
    }
}
